package org.gruposp2p.aula.gwt.client.content.course;

import java.util.ArrayList;
import java.util.List;
import org.gruposp2p.aula.gwt.client.AulaApplication;
import org.gruposp2p.aula.gwt.client.AulaConstants;
import org.gruposp2p.aula.gwt.client.AulaContentWidget;
import org.gruposp2p.aula.gwt.client.event.AulaEventBus;
import org.gruposp2p.aula.gwt.client.event.CourseChangeEvent;
import org.gruposp2p.aula.gwt.client.event.CourseChangeHandler;
import org.gruposp2p.aula.gwt.client.model.Course;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.Constants;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.ToggleButton;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class CoursePanelWidget extends AulaContentWidget 
implements CourseChangeHandler{
	
	public static interface CwConstants extends Constants {	
		String CoursePanelWidgetTabTitle();
		String CoursePanelWidgetCourseLabel();
		String CoursePanelWidgetSubjectsLabel();
	}	
	
	private AulaApplication aulaApplication;
	private AulaConstants constants;
	private CourseDataPanel courseDataPanel;
	private CourseSubjectsPanel courseSubjectsPanel;
	private ToggleButton dataToggleButton;
	private ToggleButton subjectsToggleButton;
	private List<ToggleButton> toggleButtons;
	private VerticalPanel dataPanel;

	public CoursePanelWidget (AulaApplication aulaApplication) {
		this.aulaApplication = aulaApplication;
		this.constants = aulaApplication.getConstants();
		toggleButtons = new ArrayList<ToggleButton>();
		courseDataPanel = new CourseDataPanel (aulaApplication);
		courseSubjectsPanel = new CourseSubjectsPanel (aulaApplication);
		dataToggleButton = new ToggleButton(constants.CoursePanelWidgetCourseLabel());
		dataToggleButton.addClickHandler(toggleButtonHandler);
		dataToggleButton.setStylePrimaryName("aula-ToggleButton");
		toggleButtons.add(dataToggleButton);
		subjectsToggleButton = new ToggleButton(constants.CoursePanelWidgetSubjectsLabel());
		subjectsToggleButton.addClickHandler(toggleButtonHandler);
		subjectsToggleButton.setStylePrimaryName("aula-ToggleButton");
		toggleButtons.add(subjectsToggleButton);
		AulaEventBus.getInstance().registerToCourseChange(this);
	}	

	@Override
	public String getDescription() {
		// TODO Auto-generated method stubCourseSubjectsPanel
		return null;
	}

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return constants.CoursePanelWidgetTabTitle();
	}

	@Override
	public Widget onInitialize() {
	    dataPanel = new VerticalPanel();
	    dataPanel.add(createTabbedPanel());
	    return dataPanel;
	}
	
	private void showStudent (ListBox multiBox) { 
	}

	private Widget createTabbedPanel () {		
		HorizontalPanel hPanel = new HorizontalPanel();
		hPanel.setStylePrimaryName("AulaWidgetInternalTab");
		hPanel.add(dataToggleButton);
		hPanel.add(subjectsToggleButton);
		return hPanel;		
	}

	ClickHandler toggleButtonHandler = new ClickHandler() {
		@Override
		public void onClick(ClickEvent event) {	
			ToggleButton source = (ToggleButton) event.getSource();
			for (ToggleButton toggleButton : toggleButtons) {
				if (!toggleButton.equals(source)) {
					toggleButton.setDown(false);
				} else {
					toggleButton.setDown(true);
				}
			}
		}		
	};

	@Override
	public void processCourse(CourseChangeEvent event) {
		Course course = event.getCourse();
	}
	
	@Override
	public void onLoad() {
		dataToggleButton.setDown(true);
	}
	
}